package LUCKY.Android;

import roboguice.inject.InjectView;
import LUCKY.Android.Util.FunctionsUtil;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class actUsarGPS extends ActivityLUCKY {

	@InjectView(R.id.txtLatitud) TextView txtLatitud;
	@InjectView(R.id.txtLongitud) TextView txtLongitud;
	@InjectView(R.id.button2) Button button2;
	protected static final String CLASS_NAME = "actUsarGPS";
	private String m_latitud="0";
	private String m_longitud= "0";
	private String m_crearlonlat="";
	//private int m_IdManten;
//	private String m_DNI= "";
	
	
	
	   private ProgressDialog progressDialog;
		private Handler handler;
	boolean mostrar = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usargps);

    	
    
    	//m_crearlonlat =  getIntent().getStringExtra("m_crearlonlat");
    	//m_IdManten =  getIntent().getIntExtra("m_IdManten",0);
    //	m_DNI =  getIntent().getStringExtra("m_DNI");
    	
    	txtLatitud.setText(m_latitud);
    	txtLongitud.setText(m_longitud);
    	bindata();
    	button2.setVisibility(View.GONE);
    	if(m_crearlonlat.equals(""))
	    {
    		button2.setVisibility(View.VISIBLE);
	    }
    		
	    }
	private void bindata() {
		//manten = mantenTable.fechnmanten("ID = "+ m_IdManten);
		
		
		txtLatitud.setText(m_latitud);
    	txtLongitud.setText(m_longitud);
	}
	private void localizacion() {
		progressDialog = ProgressDialog.show(
				actUsarGPS.this, "Por favor espere", "BUSCANDO", true);
		
    	 handler = new Handler();
 		
 		final Runnable runInUIThread = new Runnable() {
             public void run() {
            	 
		LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
	    LocationListener mlocListener = new MyLocationListener();
	    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
	   
             }
        };
		
        new Thread() {
             @Override
             public void run() {
                //handler.post(runInUIThread);
                handler.postDelayed(runInUIThread,1000);
                }
            }.start();
	}
		
	public class MyLocationListener implements LocationListener
	{
	@Override
	public void onLocationChanged(final Location loc)
	    {
	   
	     if(mostrar)
	     {
	    	  
	            	 
	             
	    	 loc.getLatitude();
	 	    loc.getLongitude();      
	 	    double dblLongitud = loc.getLongitude();
	 	   m_longitud = String.valueOf(dblLongitud); 
	 	   
	 	     double dblLatitud = loc.getLatitude();
	 	    m_latitud = String.valueOf(dblLatitud); 
	
	 	    txtLongitud.setText(m_longitud);
		     txtLatitud.setText(m_latitud);
		     
	       
		   
	     }
	     progressDialog.dismiss();
	     mostrar = false;
	  
	}
	@Override 
	public void onProviderDisabled(String provider)
	{

	FunctionsUtil.ShowMessage("debe de activar el GPS",Toast.LENGTH_LONG);
	mostrar = false;
	progressDialog.dismiss();
	}
	@Override 
	public void onProviderEnabled(String provider)
	{
	
	
	FunctionsUtil.ShowMessage("activado el GPS",Toast.LENGTH_LONG);
	mostrar = false;
	
	}
	@Override 
	public void onStatusChanged(String provider, int status, Bundle extras)
	{
	}
	}
	
	
	public void funciongps (View view)
	{
		    localizacion();
		    mostrar = true;
				
	}
	public void mapas (View view)
	{
		if (!m_latitud.equals("0") || !m_longitud.equals("0"))
		{
			
			Intent intent = new Intent(actUsarGPS.this, mapagooglev2.class);
			intent.putExtra("latitud", m_latitud);
			intent.putExtra("longitud", m_longitud);
			startActivity(intent);
		/*	final String zoom = "16";
		    String stringLoc = "geo:" + m_latitud + "," + m_longitud + "?z=" + zoom;
			Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(stringLoc));
		    startActivity(intent);*/
		}
		else
		{
			
			FunctionsUtil.ShowMessage("DEBE DE OBTENER LONGITUD Y LATITUD",Toast.LENGTH_LONG);
		}
		
		
		 
	}
	
	 public void guardar(View view)
	    {
		 try {
				m_latitud  = "-12.134668"; 
				m_longitud  = "-76.996976";
				txtLatitud.setText(m_latitud);
		    	txtLongitud.setText(m_longitud);
		//	 mantenTable.updatelattudlongituddni(m_DNI, m_latitud, m_longitud);
			
		} catch (Exception e) {
			// TODO: handle exception
			
	      //      FunctionsUtil.ShowMessage("No se grabaron los datos",Toast.LENGTH_SHORT);
		}
	    	
	    }
	 
	 @Override
		protected void onDestroy() {
			super.onDestroy();

		//	if (manten!=null) manten.close();
		//	if (mantenTable!=null) mantenTable.close();
	
		}
	 public void salir(View view)
	    {
		 try {
		finish();
		} catch (Exception e) {
			// TODO: handle exception
		}
	    	
	    }
	 
	
}

